package com.neusoft.nep.controller;

import com.neusoft.nep.entity.DialogManager;
import com.neusoft.nep.entity.User;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.Map;

public class UserLoginViewController {

    @FXML
    private Button btn_login;
    @FXML
    private TextField txt_userNumber;
    @FXML
    private TextField txt_userPassword;
    private DialogManager dialogManager = new DialogManager();
//用户登录
    @FXML
    public void login() {
        String phoneNumber = txt_userNumber.getText();
        String password = txt_userPassword.getText();

        if (phoneNumber.isEmpty() || password.isEmpty()) {
            DialogManager.showDialog("登录失败", "手机号和密码不能为空");
            return;
        }

        String fileName = "PublicSupervisor.txt";
        User user = null;

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
            @SuppressWarnings("unchecked")
            Map<String, User> userMap = (Map<String, User>) ois.readObject();
            user = userMap.get(phoneNumber);

            if (user != null && user.getPassword().equals(password)) {
                DialogManager.showDialog("登录成功", "欢迎回来，" + user.getRealName());

                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/PublicSupervisorView1.fxml"));
                Parent root = fxmlLoader.load();

                // 获取下一个控制器并传递手机号
                PublicSupervisorView1Controller publicSupervisorController = fxmlLoader.getController();
                publicSupervisorController.setPhoneNumber(phoneNumber);

                Scene scene = new Scene(root);
                Stage stage = new Stage();
                stage.setScene(scene);
                stage.show();

                Stage currentStage = (Stage) btn_login.getScene().getWindow();
                currentStage.close();
            } else {
                DialogManager.showDialog("登录失败", "用户不存在或密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            DialogManager.showDialog("错误", "登录过程中发生错误: " + e.getMessage());
        }
    }

    private void navigateTo(String title, String fxmlPath) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxmlPath));
            Parent root = fxmlLoader.load();
            Scene scene = new Scene(root);
            Stage stage = (Stage) btn_login.getScene().getWindow();
            stage.setTitle(title);
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
            dialogManager.showDialog("错误", "加载界面失败");
        }
    }
//开启注册页面
    @FXML
    public void register() {
        Stage stage = (Stage) btn_login.getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/UserRegisterView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Scene scene = btn_login.getScene();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }
    // 打开 FeedbackDataList.fxml 界面的独立方法
    public void openFeedbackDataList() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/FeedbackDataList.fxml"));
        try {
            Parent root = fxmlLoader.load();
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
